package com.imooc.course.business.controller.admin;

import com.imooc.course.business.form.CourseContentFileForm;
import com.imooc.course.server.dto.CourseContentFileDto;
import com.imooc.course.server.dto.ResponseDto;
import com.imooc.course.server.service.CourseContentFileService;
import com.imooc.course.server.utils.CopyUtil;
import com.imooc.course.server.utils.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/admin/courseContentFile")
public class CourseContentFileController {

    private static final Logger log = LoggerFactory.getLogger(CourseContentFileController.class);
    public static final String BUSINESS_NAME = "课程内容文件";


    @Resource
    private CourseContentFileService courseContentFileService;

    /**
     * 查询列表
     * @param courseId 传入的课程ID
     * @return
     */
    @GetMapping("/list/{courseId}")
    public ResponseDto getList(@PathVariable String courseId) {
        List<CourseContentFileDto> list = courseContentFileService.getList(courseId);
        return ResponseDto.success().setContent(list);
    }

    /**
     * 保存，id有值为更新，id无值为新增
     * @param courseContentFileForm
     * @return
     */
    @PostMapping("/save")
    public ResponseDto addCourseContentFile(@RequestBody CourseContentFileForm courseContentFileForm) {
        //入参检查
        ValidatorUtil.notNull(courseContentFileForm.getCourseId(), "课程id");
        ValidatorUtil.minimumToMaximum(courseContentFileForm.getUrl(), "地址", 1, 100);
        ValidatorUtil.minimumToMaximum(courseContentFileForm.getName(), "文件名", 1, 100);

        CourseContentFileDto courseContentFileDto = CopyUtil.copy(courseContentFileForm, CourseContentFileDto.class);
        courseContentFileService.save(courseContentFileDto);
        return ResponseDto.success().setContent(courseContentFileDto);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public ResponseDto del(@PathVariable String id) {
        courseContentFileService.delete(id);
        return ResponseDto.success();
    }
}
